import ls from 'store2'
import { identityLogin } from '~/api/identity'
export const authLogin = (obj, uri, bind) => {
  return new Promise((resolve, reject) => {
    // window.$nuxt.$vux.loading.show();
    identityLogin(obj)
      .then((res) => {
        if (res.code == '0') {
          let token = res.data.accessToken
          let userData = res.data
          let uidVoid = res.data.userVoId
          ls.set('userType', res.data.userType)
          ls.set('tokensyxk', token)
          ls.set('userVoId', uidVoid)
          ls.set('yxkData', userData)
          window.$nuxt.$vux.toast.text(res.message)
        } else {
          window.$nuxt.$vux.toast.text(res.data.message)
        }
        // window.$nuxt.$vux.loading.hide();
        resolve()
      })
      .catch((error) => {
        window.$nuxt.$vux.toast.text(error)
      })
  })
}
export const getUrlParam = (paraName) => {
  var url = decodeURIComponent(document.location.toString())
  var arrObj = url.split('?')
  if (arrObj.length > 2) {
    var arrPara = arrObj[2].split('&')
    var arr

    for (var i = 0; i < arrPara.length; i++) {
      arr = arrPara[i].split('=')

      if (arr != null && arr[0] == paraName) {
        return arr[1]
      }
    }
    return ''
  } else if (arrObj.length == 2) {
    let aryPara = arrObj[1].split('&')
    let ary
    for (let k = 0; k < aryPara.length; k++) {
      ary = aryPara[k].split('=')

      if (ary != null && ary[0] == paraName) {
        return ary[1]
      }
    }
    return ''
  } else {
    return ''
  }
}
export const getTraceId = () => {
  if (getUrlParam('trace_id')) {
    this.uuid = getUrlParam('trace_id')
    console.log('this.uuid: ', this.uuid)
    sessionStorage.setItem('trace_id', this.uuid)
    return this.uuid
  } else if (sessionStorage.getItem('trace_id')) {
    this.uuid = sessionStorage.getItem('trace_id')
    return this.uuid
  } else {
    var s = []
    var hexDigits = '0123456789abcdef'
    for (var i = 0; i < 36; i++) {
      s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1)
    }
    s[14] = '4' // bits 12-15 of the time_hi_and_version field to 0010
    s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1) // bits 6-7 of the clock_seq_hi_and_reserved to 01
    s[8] = s[13] = s[18] = s[23] = '-'

    var uuid = s.join('') + '-' + new Date().getTime()
    sessionStorage.setItem('trace_id', uuid)
    return uuid
  }
}
export const generateSxspm = (path, origin = '', eventAndParamString = '') => {
  return '微信' + '.' + path + '.' + eventAndParamString + '.' + getTraceId() + '.' + new Date().getTime() + '.' + origin
}
export const goRoutes = (routerName) => {
  window.$nuxt.$router.replace({
    path: '../' + routerName,
  })
}
export const backAppOrWx = () => {
  if (getUrlParam('gobackwx')) {
    window.$nuxt.$router.back()
  } else {
    if (checkTerminal() === 'ios') {
      window.webkit.messageHandlers.backMessage.postMessage('back')
    } else if (checkTerminal() === 'android') {
      ZMParentsJsToAndroid.backMessage('back')
    } else {
      window.$nuxt.$router.back()
    }
  }
}
export const checkTerminal = () => {
  var u = navigator.userAgent
  var chrome = u.match(/Chrome\/([\d.]+)/) || u.match(/CriOS\/([\d.]+)/)
  var isWechat = u.toLowerCase().match(/MicroMessenger/i) == 'micromessenger' || u.match(/_SQ_/i) == '_sq_'
  var isAndroid = u.match(/(Android);?[\s\/]+([\d.]+)?/)
  var isiOS = !chrome && u.match(/(iPhone|iPod|iPad).*AppleWebKit(?!.*Safari)/)
  if (isWechat) {
    return 'wechat'
  } else if (isiOS) {
    return 'ios'
  } else if (isAndroid) {
    return 'android'
  }
}
